package shop.maytinh.view;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import shop.maytinh.util.DateUtil;
import shop.maytinh.util.HibernateUtil;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import shop.maytinh.controller.MemberManager;
import shop.maytinh.model.Member;
import shop.maytinh.model.Product;

import com.opensymphony.xwork2.ActionSupport;

public class Members extends AbstractClass{
	

	
	private String user;	
	private String pass;
	
	
	private String messenger;
	
	private Member member ;
	private Member member1;
	
	public List<Member> listmember;
	
	MemberManager membermanager = new MemberManager();
	public String execute() {
	try{
			member = membermanager.getUserByUserPass(user, pass);
			if(member!=null)
			{		
				
					request.getSession().setAttribute("Member", member);
					return "success";				
			}
			else 
			{
				messenger="User & Pass Not Found...!";
				return "fail";
			}
		}	      
		catch(Exception e)
		{
			return "error";
		}
	}
	
	public String go() {
		
		return "success";
	}
	
	public String AccountGo() {
	member1 = new Member();
		try{
			member = (Member) request.getSession().getAttribute("Member");
			int a = member.getId();
			member1 = membermanager.getMemberById(a);			
			return "success";
		}
		catch(Exception e)
		{
			messenger="You are not login...!";		
			return "error";
		}
	}

	
	public String Logout() {
		
		request.getSession().removeAttribute("Member");
		request.getSession().removeAttribute("sslistcard");
		return "success";
	}
	public static String convertDateToString(Date date, String format) {
	    SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
	    return mySimpleDateFormat.format(date);
	}
	public String add() {
		try {	
			
			String format1 = "dd/MM/yyyy";
			Date d = new Date();
			String date1 = convertDateToString(d,format1);
			
			DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			Date myDate = formatter.parse(date1);
			java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
			
			member.setCreated_at(sqlDate);			
			member.setUpdated_at(sqlDate);					 		
			membermanager.add(getMember());
			messenger="User & Pass Not Found...!";		
			request.getSession().setAttribute("Member", getMember());
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return "error";		
	}
	public String UpdateAccount() {	
		try{
			String format1 = "dd/MM/yyyy";
			Date d = new Date();
			String date1 = convertDateToString(d,format1);
			
			DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			Date myDate = formatter.parse(date1);
			java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
			
			member1.setCreated_at(sqlDate);			
			member1.setUpdated_at(sqlDate);
			
			member1 = membermanager.UpdateAccount(getMember1());
			messenger = "Update Account Success...!";
			request.getSession().setAttribute("Member", getMember1());
			return "success";	
		}
		catch(Exception e)
			{		
			return "error";
			}
	}
	public String registAndLogin()
	{
		//them vao
		try {	
			
			String format1 = "dd/MM/yyyy";
			Date d = new Date();
			String date1 = convertDateToString(d,format1);
			
			DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			Date myDate = formatter.parse(date1);
			java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
			
			member.setPass("123456");
			member.setCreated_at(sqlDate);			
			member.setUpdated_at(sqlDate);					 		
			membermanager.add(getMember());
			messenger="Successful login...!";		
			request.getSession().setAttribute("Member", getMember());
			
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return "error";		
	}
	

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getMessenger() {
		return messenger;
	}

	public void setMessenger(String messenger) {
		this.messenger = messenger;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public MemberManager getMembermanager() {
		return membermanager;
	}

	public void setMembermanager(MemberManager membermanager) {
		this.membermanager = membermanager;
	}

	public Member getMember1() {
		return member1;
	}

	public void setMember1(Member member1) {
		this.member1 = member1;
	}
	
}



